↜ Back to index Introduction to Numerical Analysis 1
Solution Lecture a5
Exercise 1
! Implementation of the midpoint method for the system y_1' = y_2, y_2' = - sin(y_1)
implicit none
integer i, n
real h, t
real y(2), k(2)
real pi, theta0
read *, theta0, h
= 20 / h
n
! Initial data
= [theta0, 0.]
y print *, 0., y
do i = 1, n
= y + 0.5 * h * [y(2), -sin(y(1))]
k = y + h * [k(2), -sin(k(1))]
y
print *, i * h, y
enddo
end
1.
2.
3.
Exercise 2
! Finding the period of a physical pendulum
implicit none
integer i, j, n
real h
real y(2), k(2)
real theta0
= 0.001
h ! let's assume that the period is < 100 * 4
= 100 / h
n
do j = 1, 30
= j * 0.1
theta0 = [theta0, 0.]
y
do i = 1, n
! midpoint method
= y + 0.5 * h * [y(2), -sin(y(1))]
k = y + h * [k(2), -sin(k(1))]
y
if (y(1) <= 0.) then
! 👇 period
print *, theta0, 4. * i * h
exit
endif
enddo
enddo
end